const install = (Vue, vm) => {
    Vue.prototype.$u.http.setConfig({
        baseUrl: process.env.VUE_APP_BASE_API,
        method: 'POST',
        dataType: 'json',
        showLoading: false,
        loadingText: '努力加载中...',
        loadingTime: 800,
        //originalData: false, // 是否在拦截器中返回服务端的原始数据
        loadingMask: false, // 展示loading的时候，是否给一个透明的蒙层，防止触摸穿透
        header: {
            // 'content-type': 'application/json;charset=UTF-8'
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
    });
    //请求拦截
    Vue.prototype.$u.http.interceptor.request = (config) => {
            //参数添加third_session
            if (config.url != "/Home/Frontend/union/sign") {
                let third_session = uni.getStorageSync("loginInfo").third_session;
                config.data.third_session = third_session;
            }
            //加载loading
            let urlArray = ['/Home/Frontend/union/initUserInfo', '/Home/Frontend/edata/myTradeTotal', '/Home/Frontend/union/sign', '/Home/Frontend/eproduct/myAgentGroup'];
            if (!urlArray.includes(config.url)) {
                vm.$loading(true);
            }
            return config;
        }
        //响应拦截
    Vue.prototype.$u.http.interceptor.response = (res) => {
        //取消loading
        setTimeout(() => {
            vm.$loading(false);
        }, 10)
        if (res.status == 200) {
            return res;
        } else if (res.status == 202) {
            vm.$u.toast('登录超时，请重新登录');
            setTimeout(() => {
                uni.reLaunch({
                    url: '/pages/login/login'
                });
            }, 1500)
            return false;
        } else if (res.status == 500) {
            vm.$u.toast('系统异常');
            return false;
        } else {
            return res;
        }
    }
}

export default {
    install
}
